package com.taotao.rest.service;

import com.taotao.mapper.TbItemCatMapper;
import com.taotao.pojo.TbItemCat;
import com.taotao.pojo.TbItemCatExample;
import com.taotao.pojo.TbItemCatExample.Criteria;
import com.taotao.rest.pojo.CatNode;
import com.taotao.rest.pojo.ItemCatResult;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ItemCatServiceImpl implements ItemCatService {
	@Autowired
	private TbItemCatMapper tbItemCatMapper;

	public ItemCatResult getItemCatList() {
		List data = getItemCatList(0);
		ItemCatResult result = new ItemCatResult();
		result.setData(data);
		return result;
	}

	private List getItemCatList(long parentId) {
		TbItemCatExample example = new TbItemCatExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);

		List<TbItemCat> list = tbItemCatMapper.selectByExample(example);

		List resultList = new ArrayList();

		int index = 0;
		for (TbItemCat tbItemCat : list) {
			if (index >= 14) {
				break;
			}
			if (tbItemCat.getIsParent()) {
				CatNode node = new CatNode();
				node.setUrl("/products/" + tbItemCat.getId() + ".html");
				if (tbItemCat.getParentId().longValue() == 0) {
					node.setName("<a href='/products/" + tbItemCat.getId()
							+ ".html'>" + tbItemCat.getName() + "</a>");

					index++;
				} else {
					node.setName(tbItemCat.getName());
				}
				node.setItems(getItemCatList(tbItemCat.getId()));
				resultList.add(node);
			} else {
				String item = "/products/" + tbItemCat.getId() + ".html|"
						+ tbItemCat.getName();

				resultList.add(item);
			}
		}
		return resultList;
	}
}
